package com.msb.apipassenger.service;

import com.msb.apipassenger.remote.ServiceVerificationcodeClient;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.response.NumberCodeResponse;
import com.msb.internalcommon.response.TokenResponse;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author liukaihao
 * @date 2025/04/29
 */
@Service
public class VerificationCodeService {
    @Autowired
    private ServiceVerificationcodeClient serviceVerificationcodeClient;
    // 乘客验证码的前缀
    public String verificationCodePrefix = "passenger-verification-code-";
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 生成验证码
     * @param passengerPhone
     * @return
     */
    public ResponseResult generateCode(String passengerPhone) {

        // 调用验证码服务，获取验证码
        System.out.println("请用验证码服务，获取验证码");
        //  生成验证码
        ResponseResult<NumberCodeResponse> codeResponseResponseResult = serviceVerificationcodeClient.getNumberCode(6);
        int numberCode = codeResponseResponseResult.getData().getNumberCode();

        System.out.println("验证码remote number code ：" + numberCode);

        // 存入redis
        System.out.println("存入reids");
        // key,value,过期时间
        String key = verificationCodePrefix + passengerPhone;
        stringRedisTemplate.opsForValue().set(key, numberCode + "", 2, TimeUnit.MINUTES);
        // 通过短信服务发送验证码，将对应的验证码发送到手机，阿里短信服务，腾讯短信服务，华信短信服务，容联短信服务，


        // 返回值
        return ResponseResult.success("");
    }

    /**
     * 校验验证码
     * @param passengerPhone
     * @param verificationCode
     * @return
     */
    public ResponseResult checkCode(String passengerPhone, String verificationCode) {
        // 根据手机号，去redis中获取验证码
        System.out.println("根据手机号：，去redis读取验证码");

        // 校验验证码
        System.out.println("校验验证码");

        // 判断原来是否有用户，进行对应的处理
        System.out.println("判断原来是否有用户，进行对应的处理");

        // 颁发令牌
        System.out.println("颁发令牌");

        // 响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setToken("token value");
        return ResponseResult.success(tokenResponse);
    }
}
